package com.fywsy.fyweb.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CdkUtils {

    /**
     * 生成
     * @param type      0,普通卡密 1：期限卡
     * @param validDay  有效期天数，type=1时使用
     * @param count     生成的数量
     * @return
     */
    public static List<String> createCdk(int type, int validDay, int count) {
        List<String> cdkList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            cdkList.add(generateRandomCDK(type, validDay));
        }
        return cdkList;
    }


    // 生成一个随机的大写字母
    private static String generateRandomCDK(int type, int validDay) {
        StringBuilder cdk = new StringBuilder();
        Random random = new Random();

        // 如果是期限卡，添加有效期信息
        if (type == 1) {
            cdk.append("D");
            // 将validDay转换为字符串并补零
            String validDayStr = String.format("%03d", validDay);
            cdk.append(validDayStr);
        } else {
            // 普通卡密，生成4个随机字母
            for (int j = 0; j < 4; j++) {
                char letter = (char) ('A' + random.nextInt(26));
                cdk.append(letter);
            }
        }

        // 生成剩余的3组4位随机字母
        for (int i = 0; i < 3; i++) {
            cdk.append("-");
            for (int j = 0; j < 4; j++) {
                char letter = (char) ('A' + random.nextInt(26));
                cdk.append(letter);
            }
        }

        return cdk.toString();
    }

    public static void main(String[] args) {
        System.out.println(generateRandomCDK(1, 999));
    }
}
